using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;

namespace UltimateDefend
{
    //tham khao: http://www.spickles.org/archives/2005/12/04/90/
    public class Convert
    {
        static public byte[] GetByteArrayFromBitmap(Bitmap TheBMP)
        {
            //  First, create a memory a stream where
            //  the passed bitmap will be stored            
            MemoryStream BitmapMS = new MemoryStream();

            //  Now save the bitmap into the stream
            TheBMP.Save(BitmapMS, ImageFormat.Bmp);

            //  Create a byte array that is the same size as the
            //  memory stream that houses the recently saved bitmap
            byte[] BMPArray = new byte[BitmapMS.Length];

            //  Store the contents of the memory
            //  stream into byte array
            BMPArray = BitmapMS.ToArray();

            //  Finally, return the byte array
            return BMPArray;
        }

        public Bitmap GetBitmapFromByteArray(byte[] BMPArray)
        {
            //  First, create a memory a stream where the
            //  passed byte array will be stored

            MemoryStream BitmapMS = new MemoryStream();

            //  Write the byte array to the memory stream
            BitmapMS.Write(BMPArray, 0, BMPArray.Length);

            //  Create a new bitmap object from the memory stream
            Bitmap TheBitmap = new Bitmap(BitmapMS);

            //  Finally, return the bitmap
            return TheBitmap;
        }
    }
}
